import { User } from './../../../../../models/user';
import { ExceptionHandler } from './../../../../../handler/exception.handler';
import { Component, OnInit } from '@angular/core';
import { BaseFieldComponent } from '../base-field/base-field.component';
import { NzMessageService } from 'ng-zorro-antd';
import { BaseService, DataRegisterService } from 'src/app/services';
import { EventManagerService } from 'src/app/services/event-manager.service';
import { OrganizationService } from 'src/app/services/organization.service';
import { UploadService } from 'src/app/services/upload.service';
import { EngineService } from '../../../services/engine.service';
import { PersonnelService } from 'src/app/services/personnel.service';

@Component({
  selector: 'zepride-address-book',
  templateUrl: './address-book.component.html',
  styleUrls: ['./address-book.component.less', '../field.common.less']
})
export class AddressBookComponent extends BaseFieldComponent implements OnInit {
  sourceData: User[] = [];
  data: User[] = [];
  columnDefineList: IColumnDefineItem[] = [];
  searchData: string;
  searchPlaceholder: string = '';

  constructor(
    protected engineService: EngineService,
    protected baseSvr: BaseService,
    protected eventSvr: EventManagerService,
    protected orgSvr: OrganizationService,
    public uploadService: UploadService,
    protected msg: NzMessageService,
    private userSvr: PersonnelService,
    protected dataRegisterService: DataRegisterService
  ) {

    super(engineService, baseSvr, eventSvr, orgSvr, uploadService, msg, dataRegisterService);
  }


  async ngOnInit() {
    if (this.componentData.extends.fields) {
      this.columnDefineList = this.componentData.extends.fields.map(x => x.column);
      this.columnDefineList || (this.columnDefineList = []);
      await this.getUserList();
    }

    if (this.componentData.extends.searchKeys) {
      this.searchPlaceholder = '搜索' + this.componentData.extends.searchKeys.map(x => x.name).join('、');
    }
  }

  async getUserList() {
    try {
      const result = await this.userSvr.queryUsers({});
      this.data = result.list;

      this.sourceData = this.data;
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  search() {
    if (this.searchData) {
      const searchKeys: IColumnDefineItem[] = this.componentData.extends.searchKeys;
      if (!searchKeys || searchKeys.length == 0) {
        return;
      }
      this.data = this.sourceData.filter(x => {
        for (let item of searchKeys) {
          const itemObj = x[item.bindField];
          if (item.bindField == 'gender') {
            let genderSk = '';
            if (this.searchData == '男') genderSk = 'man';
            else if (this.searchData == '女') genderSk = 'woman';
            if (itemObj && (itemObj == this.searchData || itemObj == genderSk)) {
              return true;
            }
          }
          if (item.bindField == 'departmentNode') {
            if (itemObj && itemObj.nodeName.indexOf(this.searchData) > -1) {
              return true;
            }
          }
          if (itemObj && itemObj.indexOf && itemObj.indexOf(this.searchData) > -1) {
            return true;
          }
        }
        return false;
      });
    }
    else {
      this.data = this.sourceData;
    }
  }

  getGenderDisplay(value: string) {
    if (value == 'man') {
      return '男';
    }
    else if (value == 'woman') {
      return '女';
    }
    else {
      return value;
    }
  }
}

interface IColumnDefineItem {
  name: string;
  bindField: string;
  isImage?: boolean;
  isTime?: boolean;
}

